$(function () {

    var time = localStorage.getItem('time');

    var id = localStorage.getItem('id');
    var songListId1 = localStorage.getItem('songListId1');
    // 保存当前播放状态和歌曲信息到LocalStorage
    function savePlayStatus() {
        var playStatus = {
            currentTime: audio.currentTime,
            isPlaying: !audio.paused,
            songIndex: songIndex
        };
        localStorage.setItem("playStatus", JSON.stringify(playStatus));
    }

// 从LocalStorage中读取播放状态和歌曲信息
    function loadPlayStatus() {
        var playStatus = JSON.parse(localStorage.getItem("playStatus"));
        if (playStatus) {
            songIndex = playStatus.songIndex;
            change_music();
            audio.currentTime = playStatus.currentTime;
            if (playStatus.isPlaying) {
                audio.play();
            }
        }
    }

// 在页面卸载前保存播放状态
    window.onbeforeunload = function() {
        savePlayStatus();
    };

// 页面加载完成后读取播放状态并恢复播放
    window.onload = function() {
        loadPlayStatus();
    };
    if (null != id)
    {
        $.get(`/dev/song/bofang/${id}`, function (result) {
            var na = result.data
            // console.log(time);
            // Settings
            var isShowNotification = false,
                isInitMarquee = true,
                shuffleArray = [],
                shuffleIndex,
                autoClearTimer,
                autoShowTimer,
                isFirstPlay = localStorage.qplayer == undefined ? true : false,
                isShuffle = localStorage.qplayer == undefined ? false : localStorage.qplayer === 'true' ? true : false;

            // Load playlist
            for (var i = 0; i < na.length; i++) {

                var item = na[i];
                $('#playlist').append('<li class="lib" style="overflow:hidden;"><strong style="margin-left: 5px;">' + item.songTitle + '</strong><span style="float: right;" class="artist">' + item.singerName + '</span></li>');
                if (item.audioSrc == "") {
                    $('#playlist li').eq(i).css('color', '#ddd');
                }
            }

            var currentTrack = 0, audio, timeout;
            var shuffle_array = localStorage.qplayer_shuffle_array;

            if (isShuffle && shuffle_array != undefined && na.length === (shuffleArray = JSON.parse(shuffle_array)).length) {
                currentTrack = shuffleArray[0];
                shuffleIndex = 0;
                $('#QPlayer .cover').attr('title', '点击关闭随机播放');
            } else {
                isShuffle = false;
                $('#QPlayer .cover').attr('title', '点击开启随机播放');
            }

            //判断是否显示滚动条
            var totalHeight = 0;
            for (var i = 0; i < na.length; i++) {
                totalHeight += ($('#playlist li').eq(i).height() + 6);
            }
            if (totalHeight > 360) {
                $('#playlist').css("overflow", "auto");
                if (isShuffle) {
                    var temp = 0;
                    for (var j = 0; j < currentTrack; j++) {
                        temp += ($('#playlist li').eq(j).height() + 6);
                    }
                    $('#playlist').scrollTop(temp);
                }
            }

            var play = function () {
                audio.play();
                if (isRotate) {
                    var $coverImg = $("#player .cover img");
                    $coverImg.css("animation", "9.8s linear 0s normal none infinite rotate");
                    $coverImg.css("animation-play-state", "running");
                }
                $('.playback').addClass('playing');
                timeout = setInterval(updateProgress, 500);
                if (isExceedTag()) {
                    if (isInitMarquee) {
                        initMarquee();
                        isInitMarquee = false;
                    } else {
                        $('.marquee').marquee('resume');
                    }
                }
            }

            var pause = function () {
                audio.pause();
                $("#player .cover img").css("animation-play-state", "paused");
                $('.playback').removeClass('playing');
                clearInterval(timeout);
                if (isExceedTag()) {
                    $('.marquee').marquee('pause');
                }
            }


            // 更新项目
            var setProgress = function (value) {
                var currentSec = parseInt(value % 60) < 10 ? '0' + parseInt(value % 60) : parseInt(value % 60),
                    ratio = value / audio.duration * 100;

                $('.timer').html(parseInt(value / 60) + ':' + currentSec);
            }

            var updateProgress = function () {
                var currentTime = audio.currentTime;
                setProgress(currentTime);
            }

            // Switch track
            var switchTrack = function (i) {
                var audioElement = $('audio');
                if (i < 0) {
                    track = currentTrack = na.length - 1;
                } else if (i >= na.length) {
                    track = currentTrack = 0;
                } else {
                    track = i;
                }

                isInitMarquee = true;
                if (audioElement.attr('src') !== na[track].file) {
                    audioElement.remove();
                    loadMusic(track);
                }

                play();
            }

            // Shuffle
            var shufflePlay = function (i) {
                if (i === 1) {
                    //下一首
                    shuffleIndex = (shuffleIndex + 1) % shuffleArray.length;
                } else if (i === 0) {
                    //上一首
                    shuffleIndex = (shuffleIndex - 1 + shuffleArray.length) % shuffleArray.length;
                }
                currentTrack = shuffleArray[shuffleIndex];
                switchTrack(currentTrack);
            }

            // Fire when track ended
            var ended = function () {
                pause();
                audio.currentTime = 0;
                if (isShuffle) {
                    shufflePlay(1);
                } else {
                    if (currentTrack < na.length) switchTrack(++currentTrack);
                }

            }

            var beforeLoad = function () {
                var endVal = this.seekable && this.seekable.length ? this.seekable.end(0) : 0;
            }

            // Fire when track loaded completely
            var afterLoad = function () {

                if (autoplay == true) {

                    play();
                    // audio.currentTime=time;
                }
            }

            // Load track
            var loadMusic = function (i) {
                var item = na[i];
                while (item.audioSrc == "") {
                    showNotification('歌曲地址为空，已自动跳过');
                    if (isShuffle) {
                        if (++shuffleIndex === shuffleArray.length) {
                            shuffleIndex = 0;
                        }
                        i = currentTrack = shuffleArray[shuffleIndex];
                    } else {
                        currentTrack = ++i;
                    }
                    item = na[i];
                }
                var newaudio = $('<audio id="audi">').html('<source muted="muted" src="' + item.audioSrc + '"><source src="' + item.ogg + '">').appendTo('#player');
                $('.cover').html('<img src="' + item.imgg + '" alt="' + item.album + '">');
                $('.musicTag').html('<strong>' + item.songTitle + '</strong><span> - </span><span class="artist">' + item.singerName + '</span>');
                $('#playlist li').removeClass('playing').eq(i).addClass('playing');
                audio = newaudio[0];
                audio.addEventListener('progress', beforeLoad, false);
                audio.addEventListener('durationchange', beforeLoad, false);
                audio.addEventListener('canplay', afterLoad, false);
                audio.addEventListener('ended', ended, false);
            }

            loadMusic(currentTrack);
            var count = 0;
            $('.playback').on('click', function () {
                if ($(this).hasClass('playing')) {
                    pause();
                    if (count != 1) {
                        var time = audio.currentTime;
                        localStorage.setItem('time', time);
                    }
                } else {
                    count++;
                    if (count == 1) {
                        audio.currentTime = time;
                    }

                    play();
                    localStorage.removeItem("time");
                }
            });

            $('.rewind').on('click', function () {
                if (isShuffle) {
                    shufflePlay(0);
                } else {
                    switchTrack(--currentTrack);
                }
            });

            $('.fastforward').on('click', function () {
                if (isShuffle) {
                    shufflePlay(1);
                } else {
                    switchTrack(++currentTrack);
                }
            });

            $('#playlist li').each(function (i) {
                $(this).on('click', function () {
                    if (isShuffle) {
                        for (var j = 0; j < shuffleArray.length; j++) {
                            if (shuffleArray[j] === i) {
                                shuffleIndex = j;
                                break;
                            }
                        }
                    } else {
                        currentTrack = i;
                    }
                    switchTrack(i);
                });


            });

            $('#QPlayer .liebiao,#QPlayer .liebiao').on('click', function () {
                var pl = $('#playlist');
                if (pl.hasClass('go') === false) {
                    pl.css({"max-height": "360px", "transition": "max-height .5s ease"});
                    pl.css("border", "1px solid #dedede");
                    pl.addClass('go');
                } else {
                    pl.css({"max-height": "0px", "transition": "max-height .5s ease"});
                    pl.css("border", "0");
                    pl.removeClass('go');
                }
            });

            $("#QPlayer").css("transform", "translateX(250px)");
            $('.ssBtn .adf').addClass('on');
            $("#QPlayer .ssBtn").on('click', function () {
                var mA = $("#QPlayer");
                if ($('.ssBtn .adf').hasClass('on') === false) {
                    if (isFirstPlay) {
                        setTimeout("showTips('#player .cover','点击封面开启(关闭)随机播放', " + function () {
                            setTimeout("showTips('#player .ctrl .musicTag','点击拖动标题栏快进(快退)')", 1000)
                        } + ");", 500);
                        isFirstPlay = !isFirstPlay;
                        localStorage.qplayer = 'false';
                    }
                    mA.css("transform", "translateX(250px)");
                    $('.ssBtn .adf').addClass('on');
                } else {
                    mA.css("transform", "translateX(0px)");
                    $('.ssBtn .adf').removeClass('on')
                }
            });

            $("#player .cover").on('click', function () {
                isShuffle = !isShuffle;
                if (isShuffle) {
                    $("#player .cover").attr("title", "点击关闭随机播放");
                    showNotification('已开启随机播放');

                    var temp = [];
                    for (var i = 0; i < na.length; i++) {
                        temp[i] = i;
                    }
                    shuffleArray = shuffle(temp);
                    for (var j = 0; j < shuffleArray.length; j++) {
                        if (shuffleArray[j] === currentTrack) {
                            shuffleIndex = j;
                            break;
                        }
                    }
                    localStorage.qplayer_shuffle_array = JSON.stringify(shuffleArray);
                } else {
                    $("#player .cover").attr("title", "点击开启随机播放");
                    showNotification('已关闭随机播放');
                    localStorage.removeItem('qplayer_shuffle_array');
                }
                localStorage.qplayer = isShuffle;
            });


            var startX, endX;
            $('#player .ctrl .musicTag').mousedown(function (event) {
                startX = event.screenX;
            }).mousemove(function (event) {
                //鼠标左键
                if (event.which === 1) {
                    endX = event.screenX;
                    var seekRange = Math.round((endX - startX) / 678 * 100);
                    audio.currentTime += seekRange;
                    setProgress(audio.currentTime);

                }
            });

            $('#player .ctrl .musicTag').bind('touchstart', function (event) {
                startX = event.originalEvent.targetTouches[0].screenX;
            }).bind('touchmove', function (event) {
                endX = event.originalEvent.targetTouches[0].screenX;
                var seekRange = Math.round((endX - startX) / 678 * 100);
                audio.currentTime += seekRange;
                setProgress(audio.currentTime);
            });
        })
}
if(null!=songListId1) {
    $.get(`/dev/song/danbofang1/${songListId1}`, function (result) {
        var na = result.data
    var isShowNotification = false,
        isInitMarquee = true,
        shuffleArray = [],
        shuffleIndex,
        autoClearTimer,
        autoShowTimer,
        isFirstPlay = localStorage.qplayer == undefined ? true : false,
        isShuffle = localStorage.qplayer == undefined ? false : localStorage.qplayer === 'true' ? true : false;

    // Load playlist
    for (var i = 0; i < na.length; i++) {

        var item = na[i];
        $('#playlist').append('<li class="lib" style="overflow:hidden;"><strong style="margin-left: 5px;">' + item.songTitle + '</strong><span style="float: right;" class="artist">' + item.singerName + '</span></li>');
        if (item.audioSrc == "") {
            $('#playlist li').eq(i).css('color', '#ddd');
        }
    }

    var currentTrack = 0, audio, timeout;
    var shuffle_array = localStorage.qplayer_shuffle_array;

    if (isShuffle && shuffle_array != undefined && na.length === (shuffleArray = JSON.parse(shuffle_array)).length) {
        currentTrack = shuffleArray[0];
        shuffleIndex = 0;
        $('#QPlayer .cover').attr('title', '点击关闭随机播放');
    } else {
        isShuffle = false;
        $('#QPlayer .cover').attr('title', '点击开启随机播放');
    }

    //判断是否显示滚动条
    var totalHeight = 0;
    for (var i = 0; i < na.length; i++) {
        totalHeight += ($('#playlist li').eq(i).height() + 6);
    }
    if (totalHeight > 360) {
        $('#playlist').css("overflow", "auto");
        if (isShuffle) {
            var temp = 0;
            for (var j = 0; j < currentTrack; j++) {
                temp += ($('#playlist li').eq(j).height() + 6);
            }
            $('#playlist').scrollTop(temp);
        }
    }

    var play = function () {


        audio.play();
        // audio.currentTime=time;
        if (isRotate) {
            $("#player .cover img").css("animation", "9.8s linear 0s normal none infinite rotate");
            $("#player .cover img").css("animation-play-state", "running");
        }
        $('.playback').addClass('playing');
        timeout = setInterval(updateProgress, 500);
        //超过显示栏运行跑马灯
        if (isExceedTag()) {
            if (isInitMarquee) {
                initMarquee();
                isInitMarquee = false;
            } else {
                $('.marquee').marquee('resume');
            }
        }
    }
    var pause = function () {
        audio.pause();
        $("#player .cover img").css("animation-play-state", "paused");

        $('.playback').removeClass('playing');
        clearInterval(timeout);
        if (isExceedTag()) {
            $('.marquee').marquee('pause');
        }
    }


    // 更新项目
    var setProgress = function (value) {
        var currentSec = parseInt(value % 60) < 10 ? '0' + parseInt(value % 60) : parseInt(value % 60),
            ratio = value / audio.duration * 100;

        $('.timer').html(parseInt(value / 60) + ':' + currentSec);
    }

    var updateProgress = function () {


        setProgress(audio.currentTime)


    }

    // Switch track
    var switchTrack = function (i) {
        if (i < 0) {
            track = currentTrack = na.length - 1;
        } else if (i >= na.length) {
            track = currentTrack = 0;
        } else {
            track = i;
        }

        isInitMarquee = true;
        $('audio').remove();
        loadMusic(track);

        play();
    }

    // Shuffle
    var shufflePlay = function (i) {
        if (i === 1) {
            //下一首
            if (++shuffleIndex === shuffleArray.length) {
                shuffleIndex = 0;
            }
            currentTrack = shuffleArray[shuffleIndex];

        } else if (i === 0) {
            //上一首
            if (--shuffleIndex < 0) {
                shuffleIndex = shuffleArray.length - 1;
            }
            currentTrack = shuffleArray[shuffleIndex];
        }
        switchTrack(currentTrack);
    }

    // Fire when track ended
    var ended = function () {
        pause();
        audio.currentTime = 0;
        if (isShuffle) {
            shufflePlay(1);
        } else {
            if (currentTrack < na.length) switchTrack(++currentTrack);
        }

    }

    var beforeLoad = function () {
        var endVal = this.seekable && this.seekable.length ? this.seekable.end(0) : 0;
    }

    // Fire when track loaded completely
    var afterLoad = function () {

        if (autoplay == true) {

            play();
            // audio.currentTime=time;
        }
    }

    // Load track
    var loadMusic = function (i) {
        var item = na[i];
        while (item.audioSrc == "") {
            showNotification('歌曲地址为空，已自动跳过');
            if (isShuffle) {
                if (++shuffleIndex === shuffleArray.length) {
                    shuffleIndex = 0;
                }
                i = currentTrack = shuffleArray[shuffleIndex];
            } else {
                currentTrack = ++i;
            }
            item = na[i];
        }
        var newaudio = $('<audio id="audi">').html('<source muted="muted" src="' + item.audioSrc + '"><source src="' + item.ogg + '">').appendTo('#player');
        $('.cover').html('<img src="' + item.imgg + '" alt="' + item.album + '">');
        $('.musicTag').html('<strong>' + item.songTitle + '</strong><span> - </span><span class="artist">' + item.singerName + '</span>');
        $('#playlist li').removeClass('playing').eq(i).addClass('playing');
        audio = newaudio[0];
        audio.addEventListener('progress', beforeLoad, false);
        audio.addEventListener('durationchange', beforeLoad, false);
        audio.addEventListener('canplay', afterLoad, false);
        audio.addEventListener('ended', ended, false);
    }

    loadMusic(currentTrack);
    var count = 0;
    $('.playback').on('click', function () {
        if ($(this).hasClass('playing')) {
            pause();
            if (count != 1) {
                var time = audio.currentTime;
                localStorage.setItem('time', time);
            }
        } else {
            count++;
            if (count == 1) {
                audio.currentTime = time;
            }

            play();
            localStorage.removeItem("time");
        }
    });

    $('.rewind').on('click', function () {
        if (isShuffle) {
            shufflePlay(0);
        } else {
            switchTrack(--currentTrack);
        }
    });

    $('.fastforward').on('click', function () {
        if (isShuffle) {
            shufflePlay(1);
        } else {
            switchTrack(++currentTrack);
        }
    });

    $('#playlist li').each(function (i) {
        $(this).on('click', function () {
            if (isShuffle) {
                for (var j = 0; j < shuffleArray.length; j++) {
                    if (shuffleArray[j] === i) {
                        shuffleIndex = j;
                        break;
                    }
                }
            } else {
                currentTrack = i;
            }
            switchTrack(i);
        });


    });

    $('#QPlayer .liebiao,#QPlayer .liebiao').on('click', function () {
        var pl = $('#playlist');
        if (pl.hasClass('go') === false) {
            pl.css({"max-height": "360px", "transition": "max-height .5s ease"});
            pl.css("border", "1px solid #dedede");
            pl.addClass('go');
        } else {
            pl.css({"max-height": "0px", "transition": "max-height .5s ease"});
            pl.css("border", "0");
            pl.removeClass('go');
        }
    });

    $("#QPlayer").css("transform", "translateX(250px)");
    $('.ssBtn .adf').addClass('on');
    $("#QPlayer .ssBtn").on('click', function () {
        var mA = $("#QPlayer");
        if ($('.ssBtn .adf').hasClass('on') === false) {
            if (isFirstPlay) {
                setTimeout("showTips('#player .cover','点击封面开启(关闭)随机播放', " + function () {
                    setTimeout("showTips('#player .ctrl .musicTag','点击拖动标题栏快进(快退)')", 1000)
                } + ");", 500);
                isFirstPlay = !isFirstPlay;
                localStorage.qplayer = 'false';
            }
            mA.css("transform", "translateX(250px)");
            $('.ssBtn .adf').addClass('on');
        } else {
            mA.css("transform", "translateX(0px)");
            $('.ssBtn .adf').removeClass('on')
        }
    });

    $("#player .cover").on('click', function () {
        isShuffle = !isShuffle;
        if (isShuffle) {
            $("#player .cover").attr("title", "点击关闭随机播放");
            showNotification('已开启随机播放');

            var temp = [];
            for (var i = 0; i < na.length; i++) {
                temp[i] = i;
            }
            shuffleArray = shuffle(temp);
            for (var j = 0; j < shuffleArray.length; j++) {
                if (shuffleArray[j] === currentTrack) {
                    shuffleIndex = j;
                    break;
                }
            }
            localStorage.qplayer_shuffle_array = JSON.stringify(shuffleArray);
        } else {
            $("#player .cover").attr("title", "点击开启随机播放");
            showNotification('已关闭随机播放');
            localStorage.removeItem('qplayer_shuffle_array');
        }
        localStorage.qplayer = isShuffle;
    });


    var startX, endX;
    $('#player .ctrl .musicTag').mousedown(function (event) {
        startX = event.screenX;
    }).mousemove(function (event) {
        //鼠标左键
        if (event.which === 1) {
            endX = event.screenX;
            var seekRange = Math.round((endX - startX) / 678 * 100);
            audio.currentTime += seekRange;
            setProgress(audio.currentTime);

        }
    });

    $('#player .ctrl .musicTag').bind('touchstart', function (event) {
        startX = event.originalEvent.targetTouches[0].screenX;
    }).bind('touchmove', function (event) {
        endX = event.originalEvent.targetTouches[0].screenX;
        var seekRange = Math.round((endX - startX) / 678 * 100);
        audio.currentTime += seekRange;
        setProgress(audio.currentTime);
    });
})
}

});


function initMarquee() {
    $('.marquee').marquee({
        //speed in milliseconds of the marquee
        duration: 15000,
        //gap in pixels between the tickers
        gap: 50,
        //time in milliseconds before the marquee will start animating
        delayBeforeStart: 0,
        //'left' or 'right'
        direction: 'left',
        //true or false - should the marquee be duplicated to show an effect of continues flow
        duplicated: true
    });
}

//检测标题和作者信息总宽度是否超出播放器，超过则返回true开启跑马灯
function isExceedTag() {
    var isExceedTag = false;
    if ($('.musicTag strong').width() + $('.musicTag span').width() + $('.musicTag .artist').width() > $('.musicTag').width()) {
        isExceedTag = true;
    }
    return isExceedTag;
}


function shuffle(array) {
    var m = array.length,
        t, i;
    // 如果还剩有元素…
    while (m) {
        // 随机选取一个元素…
        i = Math.floor(Math.random() * m--);
        // 与当前元素进行交换
        t = array[m];
        array[m] = array[i];
        array[i] = t;
    }
    return array;
}

function showNotification(info) {
    isShowNotification = true;
    //判断通知是否存在，存在就移除
    if ($('.qplayer-notification').length > 0) {
        $('.qplayer-notification').remove();
        clearTimeout(autoClearTimer);
        clearTimeout(autoShowTimer);
    }
    $('body').append('<div class="qplayer-notification animation-target"><span class="qplayer-notification-icon">i</span><span class="body" style="box-shadow: rgba(0, 0, 0, 0.0980392) 0px 0px 5px;"><span class="message"></span></span><a class="close" href="#" onclick="closeNotification();return false;">×</a><div style="clear: both"></div>');
    $('.qplayer-notification .message').text(info);
    //用width:auto来自动获取通知栏宽度
    var width = $('.qplayer-notification').css({"opacity": "0", "width": "auto"}).width() + 20;
    $('.qplayer-notification').css({"width": "50px", "opacity": "1"});

    autoShowTimer = setTimeout(function () {
        $('.qplayer-notification').css({"width": width, "transition": "all .7s ease"});
        $('.qplayer-notification .close').delay(500).show(0);
    }, 1500);
    autoClearTimer = setTimeout("if ($('.qplayer-notification').length>0) {closeNotification()}", 8000);
}


function closeNotification() {
    isShowNotification = false;
    $('.qplayer-notification').css({"width": "50px", "transition": "all .7s ease"});
    $('.qplayer-notification .close').delay(500).hide(0);
    setTimeout(function () {
        if (!isShowNotification) {
            $('.qplayer-notification').css("opacity", "0");
            $('.qplayer-notification-icon').css({"transform": "scale(0)", "transition": "transform .5s ease"});
        }
    }, 1000);
    setTimeout(function () {
        if (!isShowNotification) {
            $('.qplayer-notification').remove();
        }
    }, 1500);
    clearTimeout(autoClearTimer);
    clearTimeout(autoShowTimer);
}

/*
*div: 要在其上面显示tip的div
*info: tip内容
*func: 不再提示按钮的click绑定函数
*/
function showTips(div, info, func) {
    var box_height = 100;
    $('body').append('<div class="qplayer_tips"><span class="tips_arrow"></span><span class="info" style="display:none">' + info + '</span><button class="tips_button" onclick="removeTips()">不再提示</button></div>');
    $('.qplayer_tips').css({"top": $(div).offset().top - box_height - 30 - 15, "left": $(div).offset().left - 28});
    $('.qplayer_tips').css({"height": box_height, "transition": "all .5s ease-in-out"});
    $('.qplayer_tips .info').delay(500).fadeIn();
    $('.tips_arrow').css({"border-width": "15px", "transition": "all .5s ease-in-out"});
    $('.tips_button').css({"height": "30px", "transition": "all .5s ease-in-out"});
    if (func != undefined) {
        $('.tips_button').click(func);
    }
}

function removeTips() {
    $('.qplayer_tips .info').fadeOut();
    $('.qplayer_tips').css({"height": "0", "transition": "all .5s ease-in-out"});
    $('.tips_arrow').css({"border-width": "0", "transition": "all .5s ease-in-out"});
    $('.tips_button').css({"opacity": "0", "transition": "all .2s ease-in-out"});
    setTimeout(function () {
        $('.qplayer_tips').remove()
    }, 500);
}
